<?php

namespace backend\modules\common\controllers;

use common\enums\AppEnum;
use common\traits\Curd;
use Yii;
use backend\models\ConfigCateDao;
use backend\controllers\BaseController;
use yii\data\ActiveDataProvider;




class ConfigCateController extends BaseController
{
    use Curd ;

    public $modelClass = ConfigCateDao::class;

    /**
     * 配置分类列表
     * Lists all ConfigCateDao models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
           'query' =>ConfigCateDao::find()
               ->where(['app_id'=>AppEnum::BACKEND])
               ->orderBy('sort ASC ,created_at ASC'),
            'pagination' => false
        ]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * 编辑
     * @return mixed|string|\yii\web\Response
     * @throws \yii\base\ExitException
     */
    public function actionAjaxEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id', '');
        $model = $this->findModel($id);
        $model->pid = $request->get('pid', null) ?? $model->pid; // 父id
        $model->app_id = AppEnum::BACKEND;

        // ajax 校验
        $this->activeFormValidate($model);
        if ($model->load($request->post())) {
            return $model->save()
                ? $this->redirect(['index'])
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
        }

        return $this->renderAjax('ajax-edit', [
            'model' => $model,
            'cateDropDownList' => Yii::$app->services->configCate->getDropDownForEdit(AppEnum::BACKEND, $id),
        ]);
    }




}
